package com.dean.action;

import com.dean.entity.*;
import com.dean.exception.DeanRuntimeException;
import com.dean.service.CodeGenerateService;
import com.dean.service.DataFileImpExpCfgService;
import com.dean.service.DataFileImpExpService;
import com.dean.service.DeanExecutorService;
import com.dean.service.impl.CsvImpExpServiceImpl;
import com.dean.service.impl.ExcelImpExpServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @ClassName EntityGenAction
 * @Description entity生成控制
 * @Author Administrator
 * @Date 2021/10/2315:31
 */
public class DataFileImpExpAction {
    private final Logger logger = LoggerFactory.getLogger(DataFileImpExpAction.class);

    private static class CodeGenActionHolder{
        private final static DataFileImpExpAction INSTANCE = new DataFileImpExpAction();
    }

    public static DataFileImpExpAction getInstance(){
        return DataFileImpExpAction.CodeGenActionHolder.INSTANCE;
    }

    public DeanResponse<String> saveCfg(DataFileImpExpEntity entity){
        try {
            DataFileImpExpCfgService.getInstance().saveCfg(entity);
        }catch (Exception e){
            if(e instanceof DeanRuntimeException){
                DeanRuntimeException exception = (DeanRuntimeException) e;
                return DeanResponse.error(exception.getCode(),exception.getMsg());
            }else{
                logger.error(e.getMessage(),e);
                return DeanResponse.error();
            }
        }
        return DeanResponse.ok("操作成功");
    }

    public DeanResponse<String> delCfg(DataFileImpExpEntity entity){
        try {
            DataFileImpExpCfgService.getInstance().delCfg(entity);
        }catch (Exception e){
            if(e instanceof DeanRuntimeException){
                DeanRuntimeException exception = (DeanRuntimeException) e;
                return DeanResponse.error(exception.getCode(),exception.getMsg());
            }else{
                logger.error(e.getMessage(),e);
                return DeanResponse.error();
            }
        }
        return DeanResponse.ok("操作成功");
    }

    public DeanResponse<String> updateCfg(DataFileImpExpEntity entity){
        try {
            DataFileImpExpCfgService.getInstance().updateCfg(entity);
        }catch (Exception e){
            if(e instanceof DeanRuntimeException){
                DeanRuntimeException exception = (DeanRuntimeException) e;
                return DeanResponse.error(exception.getCode(),exception.getMsg());
            }else{
                logger.error(e.getMessage(),e);
                return DeanResponse.error();
            }
        }
        return DeanResponse.ok("操作成功");
    }
    // 执行操作
    public DeanResponse<String> execute(DataFileImpExpEntity entity){
        try {
            String optType = entity.getOptType();
            String fileType = entity.getFileType();
            if("文件导入".equals(optType)){
                DataFileImpExpService service = null;
                if("EXCEL".equals(fileType)){
                    service = new ExcelImpExpServiceImpl();
                }else if("TXT".equals(fileType)){
                    service = new CsvImpExpServiceImpl();
                }else{
                    return DeanResponse.error(996,"非法操作,不支持的文件格式");
                }
                DeanImportInfo importInfo = new DeanImportInfo();
                importInfo.setFilePath(entity.getFilePath());
                importInfo.setTableName(entity.getTableName());
                importInfo.setDelimiter(entity.getDelimiter());
                importInfo.setConnName(entity.getConnName());
                importInfo.setFields(entity.getExeCfg().split(","));
                importInfo.setCommitNums(Integer.valueOf(entity.getCommitNum()));
                DeanExecutorService.exeFileImp(service,importInfo);
            }else if("文件导出".equals(optType)){
                DataFileImpExpService service = null;
                if("EXCEL".equals(fileType)){
                    service = new ExcelImpExpServiceImpl();
                }else if("TXT".equals(fileType)){
                    service = new CsvImpExpServiceImpl();
                }else{
                    return DeanResponse.error(996,"非法操作,不支持的文件格式");
                }
                DeanExportInfo exportInfo = new DeanExportInfo();
                exportInfo.setFilePath(entity.getFilePath());
                exportInfo.setConnName(entity.getConnName());
                exportInfo.setDelimiter(entity.getDelimiter());
                exportInfo.setQuerySql(entity.getExeCfg());
                DeanExecutorService.exeFileExp(service,exportInfo);
            }else{
                return DeanResponse.error(996,"非法操作,不支持的选项");
            }
        }catch (Exception e){
            if(e instanceof DeanRuntimeException){
                DeanRuntimeException exception = (DeanRuntimeException) e;
                return DeanResponse.error(exception.getCode(),exception.getMsg());
            }else{
                logger.error(e.getMessage(),e);
                return DeanResponse.error();
            }
        }
        return DeanResponse.ok("操作成功");
    }
}
